package com.ruoyi.web.controller.manager.common;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.msg.ResponseCode;
import com.returns.value.ICodeAndData;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.oss.config.OssConfig;
import com.ruoyi.oss.config.param.FileData;
import com.ruoyi.oss.config.param.FileParam;
import com.ruoyi.oss.service.OssService;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Oss对象存储管理Controller Created by macro on 2018/4/26.
 */
@RestController
@Api(tags = "OssController", description = "Oss管理")
@RequestMapping("/oss")
public class OssController {
	private OssService ossService;
	
	public OssController(OssService ossService) {
		this.ossService = ossService;
	}

//	@ApiOperation(value = "图片Oss图片上传签名生成")
//	@RequestMapping(value = "/policy", method = RequestMethod.POST)
//	@ResponseBody
//	public ResponseModel<String> policy(@Validated @RequestBody FileData fileParam) throws IOException {
//		String result = ossService.policy(OssConfig::getPicPrefix, fileParam.getFileName());
//		return ResponseModel.success(result);
//	}
//	
//	@ApiOperation(value = "图片上传分段")
//	@RequestMapping(value = "/uploadSegment", method = RequestMethod.POST)
//	@ResponseBody
//	public ResponseModel<String> uploadSegment(@Validated @RequestBody UploadSegmentParam fileParam) throws IOException {
//		String result = ossService.uploadSegment(OssConfig::getPicPrefix, fileParam.getFileName(),fileParam);
//		return ResponseModel.success(result);
//	}
//	
//	
//	@ApiOperation(value = "图片合并段")
//	@RequestMapping(value = "/mergeSegment", method = RequestMethod.POST)
//	@ResponseBody
//	public ResponseModel<String> mergeSegment(@Validated @RequestBody UploadId uploadId) throws IOException {
//		String result = ossService.mergeSegment(OssConfig::getPicPrefix, uploadId);
//		return ResponseModel.success(result);
//	}
//	
//	@ApiOperation(value = "列举图片已上传段")
//	@RequestMapping(value = "/listAlreadyUploadSegment", method = RequestMethod.POST)
//	@ResponseBody
//	public ResponseModel<String> listAlreadyUploadSegment(@Validated @RequestBody UploadId uploadId) throws IOException {
//		String result = ossService.listAlreadyUploadSegment(OssConfig::getPicPrefix, uploadId);
//		return ResponseModel.success(result);
//	}
//	
//	
//	@ApiOperation(value = "图片上传分段验证文件类型")
//	@RequestMapping(value = "/UploadSegmentParamCheckFileHeader", method = RequestMethod.POST)
//	@ResponseBody
//	public ResponseModel<String> UploadSegmentParamCheckFileHeader(@Validated @RequestBody UploadSegmentParamCheckFileHeader fileParam) throws IOException {
//		ICodeAndData<String> result = ossService.uploadSegmentCheckFileHeader(OssConfig::getPicPrefix, fileParam.getFileName(),fileParam);
//		return ResponseModel.response(result);
//	}
//	
	/**
	 * 上传服务图片签名
	 * @param file
	 * @return
	 */
	@PostMapping("/uploadPicEmpower")
	public AjaxResult uploadPicEmpower(@RequestBody @Validated FileData file) {
		
		ICodeAndData<Map<String, Object>> codeAndData = this.ossService.signURL(file.getFileName(),
				file.getType(),
				OssConfig::getPicPrefix);
		
		return responseUploadInfo(codeAndData);
	}
	
	/**
	 * 上传新闻图片
	 * @param file
	 * @return
	 */
	@PostMapping("/uploadPicNews")
	public AjaxResult uploadPicNews(@RequestBody @Validated FileData file) {
		
		ICodeAndData<Map<String, Object>> codeAndData = this.ossService.signURL(file.getFileName(),
				file.getType(),
				OssConfig::getPicPrefix);
		
		return responseUploadInfo(codeAndData);
	}
	
	/**
	 * 上传新闻图片
	 * @param file
	 * @return
	 */
	@PostMapping("/uploadPicExample")
	public AjaxResult uploadPicExample(@RequestBody @Validated FileData file) {
		
		ICodeAndData<Map<String, Object>> codeAndData = this.ossService.signURL(file.getFileName(),
				file.getType(),
				OssConfig::getExample);
		
		return responseUploadInfo(codeAndData);
	}
	
	
	@PostMapping("/uploadResourcePic")
	public AjaxResult uploadResourcePic(@RequestBody @Validated FileData file) {
		
		ICodeAndData<Map<String, Object>> codeAndData = this.ossService.signURL(file.getFileName(),
				file.getType(),
				OssConfig::getResource);
		
		return responseUploadInfo(codeAndData);
	}
	/**
	 * 上传翻译证明
	 * @param file
	 * @return
	 */
	@PostMapping("/uploadTranslateFile")
	public AjaxResult uploadTranslateFile(@RequestBody @Validated FileData file) {
		
		ICodeAndData<Map<String, Object>> codeAndData = this.ossService.signURL(file.getFileName(),
				file.getType(),
				OssConfig::getPaymentApproval);
		
		return responseUploadInfo(codeAndData);
	}
	
	/**
	 * 上传banner
	 * @param file
	 * @return
	 */
	@PostMapping("/uploadBanner")
	public AjaxResult uploadBanner(@RequestBody @Validated FileData file) {
		
		ICodeAndData<Map<String, Object>> codeAndData = this.ossService.signURL(file.getFileName(),
				file.getType(),
				OssConfig::getFollowPic);
		
		return responseUploadInfo(codeAndData);
	}
	
	/**
	 * 上传翻译协会图片
	 * @param file
	 * @return
	 */
	@PostMapping("/uploadTranslatePic")
	public AjaxResult uploadTranslatePic(@RequestBody @Validated FileData file) {
		
		ICodeAndData<Map<String, Object>> codeAndData = this.ossService.signURL(file.getFileName(),
				file.getType(),
				OssConfig::getModulePrefix);
		
		return responseUploadInfo(codeAndData);
	}
	
	/**
	 * 上传拟账合同
	 * @param file
	 * @return
	 */
	@PostMapping("/uploadContract")
	public AjaxResult uploadContract(@RequestBody @Validated FileData file) {
		
		ICodeAndData<Map<String, Object>> codeAndData = this.ossService.signURL(file.getFileName(),
				file.getType(),
				OssConfig::getOrderApproval);
		
		return responseUploadInfo(codeAndData);
	}
	
	
	
	/**
	 * 上传拟账合同
	 * @param file
	 * @return
	 */
	@PostMapping("/uploadPicActity")
	public AjaxResult uploadPicActity(@RequestBody @Validated FileData file) {
		
		ICodeAndData<Map<String, Object>> codeAndData = this.ossService.signURL(file.getFileName(),
				file.getType(),
				OssConfig::getReceivables);
		
		return responseUploadInfo(codeAndData);
	}
	
	
	
	/**
	 * 上传拟账合同
	 * @param file
	 * @return
	 */
	@PostMapping("/uploadTranslate")
	public AjaxResult uploadTranslate(@RequestBody @Validated FileData file) {
		
		ICodeAndData<Map<String, Object>> codeAndData = this.ossService.signURL(file.getFileName(),
				file.getType(),
				OssConfig::getProductImag);
		
		return responseUploadInfo(codeAndData);
	}
	

	private AjaxResult responseUploadInfo(ICodeAndData<Map<String, Object>> codeAndData) {
		ResponseCode responseCode = codeAndData.getResponseCode();
		
		if (!responseCode.equals(ResponseCode.SUCCESS)) {
			return AjaxResult.error(responseCode.getMsg());
		}
		
		return  AjaxResult.success(responseCode.getMsg(), codeAndData.getData());
	}
	
	
	/**
	 * 上传稿件
	 * @param file
	 * @return
	 */
	@PostMapping("/uploadManuscripts")
	@ResponseBody
	public AjaxResult uploadManuscripts( @RequestBody @Validated FileData file) {
		
		ICodeAndData<Map<String, Object>> codeAndData = this.ossService.signURL(file.getFileName(),
				file.getType(),
				OssConfig::getOrder);
		
		return responseUploadInfo(codeAndData);
	}
	
	
	
	
	
	/**
	 * 上传角色图片
	 * @param file
	 * @return
	 */
	@PostMapping("/uploadPic")
	@ResponseBody
	public AjaxResult uploadPic( MultipartFile file) {
		
		ICodeAndData<String> codeAndData = this.ossService.uploadFileNFT(OssConfig::getPicPrefix, file);
		
		ResponseCode responseCode = codeAndData.getResponseCode();
		
		if (!responseCode.equals(ResponseCode.SUCCESS)) {
			return AjaxResult.error(responseCode.getMsg());
		}
		
		return  AjaxResult.success(responseCode.getMsg(), codeAndData.getData());
	}
	
	
	
	
	@ApiOperation(value = "删除对象")
	@PostMapping(value = "/delete")
	@ResponseBody
	public AjaxResult delete(@RequestBody @Validated FileParam file) throws IOException {
		
		List<String> fileList = new ArrayList<>();
		
		String fileRes =  file.getFileName();
		
		fileList.add(fileRes);
		return AjaxResult.success(this.ossService.deleteFileToOSS(fileList));
	}

}
